/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Copyright (C) 2003-05  Jimm Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/jimm/Emotions.java
 Version: ###VERSION###  Date: ###DATE###
 Author(s): Artyomov Denis
 *******************************************************************************/

package jimm;

//#sijapp cond.if modules_SMILES is "true" #

import java.util.Vector;
import javax.microedition.lcdui.*;

import java.io.*;
import jimm.comm.*;
import jimm.util.ResourceBundle;
import DrawControls.*;

public class XTrazTable implements VirtualListCommands, CommandListener
{
	private static XTrazTable _this; 
//	final private static ImageList images = new ImageList();
	private static int[] selXtrazIndexes, textCorrIndexes;
	private static String[] selXtrazNames;
	private static int selectedStatus;

	public static int xstIndex;


	public XTrazTable()
	{
	   try 
           {
		_this = this;
		
		// Read images
//		images.load("/xstatus.png");//, 16, 16, -1);

//		images = XStatus.imageList;
		System.gc();

 		selXtrazIndexes = new int[XStatus.imageList.size()-1];
 		selXtrazNames= new String[XStatus.imageList.size()-1];

                selXtrazIndexes[0] = 36;
                
 		for (int i = 0; i < XStatus.imageList.size()-2; i++)
		{
			selXtrazIndexes[i+1] = i;
			selXtrazNames[i+1] = Options.getXtrazTitle(i); //XStatus.getStatusAsString(i-1);
		}
                selXtrazNames[0] = Options.getXtrazTitle(36);
 	   }
           catch (Exception e) {}
	}
	
	
	///////////////////////////////////
	//                               // 
	//   UI for emotion selection    //
	//                               //
	///////////////////////////////////
	
	static private Displayable lastDisplay;
//	static private CommandListener selectionListener;
	static private Command cmdOk = new Command(ResourceBundle.getString("select"), Command.OK, 1); 
	//#sijapp cond.if target is "MIDP2"#
	static private Command cmdCancel = new Command(ResourceBundle.getString("cancel"), Jimm.is_smart_SE() ? Command.CANCEL : Command.BACK, 2); 
	//#sijapp cond.else#
	static private Command cmdCancel = new Command(ResourceBundle.getString("cancel"), Command.BACK, 2); 
	//#sijapp cond.end#

	static private Selector selector;

	static public void selectXtraz(/*CommandListener selectionListener_,*/ Displayable lastDisplay_)
	{

		lastDisplay       = lastDisplay_;
		selector = new Selector();
		JimmUI.setColorScheme(selector);

		selector.setCurrentItm(Options.getInt(Options.OPTION_XSTATUS)+1);

		
		// #sijapp cond.if target is "MIDP2" | target is "MOTOROLA" | target is "SIEMENS2"#
		selector.setFullScreenMode(true);
		// #sijapp cond.end#
		
//		selector.addCommand(cmdOk);
//		selector.addCommand(cmdCancel);
//		selector.setCommandListener(_this);
		
		Jimm.display.setCurrent(selector);
	}
	
	public void commandAction(Command c, Displayable d)
	{
		if (c == cmdOk) select(1);
		else if (c == cmdCancel)
		{
			Jimm.display.setCurrent(lastDisplay);
			selector = null;
		};
	}
	
	public boolean onKeyPress(VirtualList sender, int keyCode,int type) 
	{        
		return false;

   /*
		switch (getGameAction(keyCode))
		{
			case Canvas.LEFT:  moveCursor(-1, false); break;
			case Canvas.RIGHT: moveCursor(1, false); break;
		} */

	}

	public void onCursorMove(VirtualList sender) {}
	public void onItemSelected(VirtualList sender) 
	{
		select(1);
	}

	static private void select(int n)
	{
//		NUM = n;
//		Jimm.display.setCurrent(lastDisplay);        
		selector = null;
		System.gc();
		//selectionListener.commandAction(cmdOk, selector);

                xstIndex = selectedStatus;
                if (xstIndex == 0) 
                {
                    Options.setInt(Options.OPTION_XSTATUS, XStatus.XSTATUS_NONE);
					Options.safe_save();
			{
                /* Active MM/CL */
                if (Icq.isConnected()) 
                {
                    ContactList.activate();
                } 
                else 
                {
                    //MainMenu.rebuild();
                    //MainMenu.activate();
			Jimm.gm.refreshMainMenu();
			Jimm.display.setCurrent(lastDisplay);
                }
            }



					if (Icq.isConnected()) 
					{
						try 
						{
							OtherAction.setStandartUserInfo();
						} 
						catch (JimmException e) 
						{
							JimmException.handleException(e);
						}
					}
				} 
				else 
				{
					XStatusForm.activate();
				}

	}
	
	static public int getSelectedXtraz()
	{
		return selectedStatus;
	}
	
	static public boolean isMyOkCommand(Command command)
	{
		return (command == cmdOk);
	}
	
	
	
	/////////////////////////
	//                     //
	//    class Selector   //
	//                     //
	/////////////////////////
		
	static class Selector extends VirtualList implements VirtualListCommands
	{
		static private int cols, rows, itemHeight, curCol;
		static private Selector _this;

		public void setCurrentItm(int itm)
		{
//			System.out.print("itm="); System.out.println(itm);
//			System.out.print("col="); System.out.println(itm%cols);
//			System.out.print("row="); System.out.println((itm)/cols);

			if (itm == 38)
				itm = 0;

			curCol = itm % cols;
			setCurrentItem(itm / cols);
			//System.out.println(itm / cols);
		}

		Selector()
		{
			super(null);
			_this = this;
			setVLCommands(this);
			
			int drawWidth = getWidth()-scrollerWidth-2;
			
			setCursorMode(SEL_NONE);
			
			int imgHeight = XStatus.imageList.getHeight() + (XStatus.imageList.getHeight() / 2);
			
			itemHeight = imgHeight+2;
			
			cols = drawWidth/itemHeight;
			rows = (selXtrazIndexes.length+cols-1)/cols;
			curCol = 0;

			rightCaption = ResourceBundle.getString("back");
			leftCaption = ResourceBundle.getString("select");


			IsItATable = true;
			
			showCurrSmileName();
			setFullScreenMode(true);
		}
		
		//#sijapp cond.if target is "MIDP2"#
		protected boolean pointerPressedOnUtem(int index, int x, int y)
		{
			int lastCol = curCol; 
			curCol = x/itemHeight;
			if (curCol < 0) curCol = 0;
			if (curCol >= cols) curCol = cols-1;
			if (lastCol != curCol)
			{
				showCurrSmileName();
				invalidate();
			}
				
			return false;
		}
		// #sijapp cond.end#
		
		protected void drawItemData
		(
			Graphics g, 
		    boolean isSelected, 
		    int index, 
		    int x1, int y1, int x2, int y2,
		    int fontHeight
		)
		{
			int xa, xb;
			int startIdx = cols*index;
			int imagesCount = XStatus.imageList.size();
			xa = x1;
			for (int i = 0; i < cols; i++, startIdx++)
			{
				if (startIdx >= selXtrazIndexes.length) break;
				int smileIdx = selXtrazIndexes[startIdx]; 
				
				xb = xa+itemHeight;
				
		
				if (isSelected && (i == curCol))
				{
					g.setColor(Options.getSchemeColor(Options.CLRSCHHEME_CURSOR));

					if (Options.getBoolean(Options.OPTION_DOTTED_CURSOR))
					{
						g.setStrokeStyle(Graphics.DOTTED);
//						g.setColor(this.getTextColor());
                                                g.drawRect(xa, y1, itemHeight-1, y2-y1-1);
					} else
					{
//						g.setColor(Options.getInt(Options.OPTION_COLOR_CURSOR)/*cursorColor*/);
		        			g.fillRect(xa, y1, itemHeight-1, y2-y1-1);
					}

				}

				if (smileIdx < imagesCount)
//					g.drawImage(images.elementAt(smileIdx), xa+1, y1+1, Graphics.TOP|Graphics.LEFT);
					g.drawImage(XStatus.imageList.elementAt(smileIdx), xa+1+XStatus.imageList.getHeight() / 4, y1+1+XStatus.imageList.getHeight() / 4, Graphics.TOP|Graphics.LEFT); //Graphics.TOP|Graphics.LEFT);


				xa = xb;
			}
		}
		
		static private void showCurrSmileName()
		{
			int selIdx = _this.getCurrIndex()*cols+curCol;
			if (selIdx >= selXtrazNames.length) return;
//			emotionText = selEmotionsWord[selIdx];			
			selectedStatus = selIdx;
			_this.setCaption(selXtrazNames[selIdx]);
		}
		
		public int getItemHeight(int itemIndex)
		{
			return itemHeight;
		}
		
		protected int getSize()
		{
			return rows;
		}
		
		protected void get(int index, ListItem item)
		{
			
		}
		
		public boolean onKeyPress(VirtualList sender, int keyCode, int type) 
		{
			if (type == VirtualList.KEY_PRESSED || type == VirtualList.KEY_REPEATED)
			{
			int lastCol = curCol;
			int curRow = getCurrIndex();
			int rowCount = getSize();


/*
			int selIdx = _this.getCurrIndex()*cols+curCol;
			if (selIdx >= selEmotionsSmileNames.length) return;
			emotionText = selEmotionsWord[selIdx];			
			_this.setCaption(Integer.toString(keyCode));
*/			
/*
			if (keyCode == 52)
			{
				select(2);
				return;
			}
			if (keyCode == 54)
			{
				select(3);
				return;
			}

*/
                        if (keyCode == Jimm.leftSoftKey)
			{
				select(1);
				return true;
			} else
			if (keyCode == Jimm.rightSoftKey || ((Jimm.is_phone_SE()) && (keyCode == -11)))
			{
				Jimm.gm.showMainMenu(); 
				return true;
			}

			switch (getGameAction(keyCode))
			{
/*
			case LEFT:
				if (curCol != 0) curCol--;
				else if (curRow != 0)
				{
					curCol = cols-1;
					curRow--;
				}
				break;
				
			case RIGHT:	
				if (curCol < (cols-1)) curCol++;
				else if (curRow <= rowCount)
				{
					curCol = 0;
					curRow++;
				}
				break;
*/

			case LEFT:
				if (curCol != 0) curCol--;
				else if (curRow > 0)
				{
					curCol = cols-1;
					curRow--;
				} else
				{
					if (Options.getBoolean(Options.OPTION_FLIP_CURSOR))
					{
						curCol = cols - 1;
						curRow = rowCount - 1;
					}

				}	
				break;
				//return true;
				
			case RIGHT:
				//System.out.println(curRow);
//System.out.println(cols);                     cnt=37  cols=6
//System.out.println(rowCount);
				
//				System.out.println(cnt);
//				System.out.println(cols);

				if ((curCol == 36%cols) && (curRow == rowCount-1))
				{ 
				if (Options.getBoolean(Options.OPTION_FLIP_CURSOR))
				{
					curCol = 0;
					curRow = 0;
				}
				//return true;
				}


				if (curCol < (cols-1)) curCol++;
				else if (curRow < rowCount - 1)
				{
					curCol = 0;
					curRow++;
				} else
				{
				}
				break;
				//return true;



/*
			case KEY_NUM6:
			{
				select(3);		
				return;
			}
*/
			}
			
			setCurrentItem(curRow);
			
			int index = curCol+getCurrIndex()*cols;
			if (index >= selXtrazIndexes.length) curCol = (selXtrazIndexes.length-1)%cols; 
			
			if (lastCol != curCol)
			{
				invalidate();
				showCurrSmileName();
			}
			
		}
			return false;
		}
		
		public void onCursorMove(VirtualList sender) 
		{
			showCurrSmileName();
		}
		
		public void onItemSelected(VirtualList sender) 
		{
			select(1);
		}
	}
}

//#sijapp cond.end#
